﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Windows.Media.Animation;

namespace WpfApplication1.Views
{
	/// <summary>
	/// Interaction logic for FriendView.xaml
	/// </summary>
	public partial class FriendView : UserControl
	{
        static FriendView chossen;
        public WindowsPostMessage myPostMessageDialog;

		private static DependencyProperty MyBorderBrushProperty = DependencyProperty.Register(
            "BorderBrushColor", typeof(Brush), typeof(FriendView));
		public Brush BorderBrushColor
		{
			get { return (Brush)GetValue(MyBorderBrushProperty); }
			set { SetValue(MyBorderBrushProperty, value); }
		}
		
		public FriendView()
		{
			this.InitializeComponent();
		}

        public void CollapInfo()
        {
            if (ExtraInfo.Visibility != Visibility.Collapsed)
            {
                if (myPostMessageDialog != null)
                    myPostMessageDialog.Close();
                ExtraInfo.Visibility = Visibility.Collapsed;
                buttonsPanel.Visibility = Visibility.Collapsed;
                // Animation het loi rui :))
                DoubleAnimation anima = new DoubleAnimation();
                anima.Duration = new TimeSpan(0, 0, 0, 0, 200);
                anima.From = this.ActualHeight;
                anima.To = 72;
                anima.FillBehavior = FillBehavior.HoldEnd;
                anima.AutoReverse = false;
                this.BeginAnimation(Window.HeightProperty, anima);
            }
            //MessageBox.Show(ExtraInfo.ActualHeight.ToString());
        }

        private void ShowInfo()
        {
            // TODO: Add event handler implementation here.
            //MessageBox.Show(ExtraInfo.ActualHeight.ToString() + " SizeChanged");
            if (ExtraInfo.Visibility != Visibility.Visible)
            {
                ExtraInfo.Visibility = Visibility.Visible;
                DoubleAnimation anima = new DoubleAnimation();
                anima.Duration = new TimeSpan(0, 0, 0, 0, 200);
                anima.From = this.ActualHeight;
                buttonsPanel.Visibility = Visibility.Visible;
                this.UpdateLayout();
                anima.To = 72 - _profilename.ActualHeight + ExtraInfo.ActualHeight;
                anima.FillBehavior = FillBehavior.HoldEnd;
                anima.AutoReverse = false;
                this.BeginAnimation(Window.HeightProperty, anima);
            }
        }

        private void UserControl_MouseLeftButtonDown(object sender, System.Windows.Input.MouseButtonEventArgs e)
        {
            // TODO: Add event handler implementation here.

            if (chossen != this)
            {
                if (chossen != null)
                {
                    //MessageBox.Show("Thu nho thang nay lai "+chossen.Id);
                    if (chossen.myPostMessageDialog != null)
                        chossen.myPostMessageDialog.Close();
                    chossen.CollapInfo();
                }
            }
            chossen = this;
            // Dien thong tin
            var button = sender as FriendView;
            if (button != null)
            {
                var command = button.Tag as ICommand;
                if (command != null)
                    command.Execute(null);
            }
            ShowInfo();
        }

        private void button_postwall_Click(object sender, RoutedEventArgs e)
        {
                Point rootPoint = Mouse.GetPosition(Application.Current.MainWindow);
                string id = hiddenId.Text; ;

                myPostMessageDialog = new WindowsPostMessage(rootPoint.Y, id, MainWindow.AccessToken);
                myPostMessageDialog.Show();
        }

	}
}